﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace CRM
{
    public partial class UserService : System.Web.UI.Page
    {
        protected override void OnPreLoad(EventArgs e)
        {
            Common.CheckMes.CheckState("User_ID", "Index.aspx", "用户登录信息丢失，青重新登录...");
            //取得权限信息
            string PowerMes=Common.EncryptString.DeMyEnncrypt(Common.GetMes.GetCookies("Power_Mes"),6);
            if (PowerMes.IndexOf("1_3")<0)
            {
                BtnAllDel.Enabled = false;
                BtnUpdate.Enabled = false;
            }

            base.OnPreLoad(e);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadService();
                LoadData();
            }
        }
         
        private void LoadData()
        {
            //实例化用来作为参数对象的DataTogther类
            Model.DataTogther ModelTo = new CRM.Model.DataTogther();
            ModelTo.TableName = " T_UserInfo ";           //指定表名
            ModelTo.PageSize = PagerUserService.PageSize; //指定分页尺寸
            ModelTo.PageIndex = PagerUserService.CurrentPageIndex; //当前页尺寸
            ModelTo.PrimaryKey = " T_UserID ";            //主键ID号
            ModelTo.SortType = 3;                         //排序类型
            //排序中一定要包含主键 否则报错“无法找到表0”
            ModelTo.Order = " T_DateTime DESC,T_UserID DESC ";
            ModelTo.FieldList = " T_UserID,T_Person,T_CompanyName,T_DateTime," +
                                "T_UserType,T_UserLeave,T_UserFrom,T_UserState,T_NowService ";
            ModelTo.Where = GetWhere();                   //获取查询条件
            ModelTo.RecorderCount = 0;                    //初始化记录条数
            int RecordCount=0;
            //指定Repeater的数据源
            RpUserService.DataSource = BLL.SysBase.GetDataList(ModelTo, out RecordCount);
            RpUserService.DataBind();                   //执行数据绑定
            PagerUserService.RecordCount = RecordCount; //指定AspNetPager的记录行数
        }

        protected void PagerUserService_PageChanged(object sender, EventArgs e)
        {
            LoadData();
        }
        /// <summary>
        /// 获取查询条件
        /// </summary>
        private string GetWhere()
        {
            StringBuilder strWhere = new StringBuilder();
            strWhere.Append((string.IsNullOrEmpty(txtSearchKey.Value.Trim())) ? "" : string.Format(" T_Person like '%{0}%' or T_CompanyName like '%{0}%'", txtSearchKey.Value.Trim()));
            strWhere.Append((DDlState.SelectedValue == "-1") ? "" : string.Format((string.IsNullOrEmpty(strWhere.ToString()) ? "" : " AND ") + " T_UserState={0} ", DDlState.SelectedValue));
            strWhere.Append((DDlClass.SelectedValue == "-1") ? "" : string.Format((string.IsNullOrEmpty(strWhere.ToString()) ? "" : " AND ") + " T_UserType={0} ", DDlClass.SelectedValue));
            strWhere.Append((DDLLeave.SelectedValue == "-1") ? "" : string.Format((string.IsNullOrEmpty(strWhere.ToString()) ? "" : " AND ") + " T_UserLeave={0} ", DDLLeave.SelectedValue));
            strWhere.Append((DDlService.SelectedValue == "-1") ? "" : string.Format((string.IsNullOrEmpty(strWhere.ToString()) ? "" : " AND ") + " T_NowService='{0}' ", DDlService.SelectedItem.Text.Trim()));
            strWhere.Append((string.IsNullOrEmpty(Province.Value) || Province.Value == "全国") ? "" : string.Format((string.IsNullOrEmpty(strWhere.ToString()) ? "" : " AND ") + " T_Province='{0}' ", Province.Value.Trim()));
            //先屏蔽按照地区查询 因为导入数据中，几乎都没有所属地区
            //strWhere.Append((string.IsNullOrEmpty(City.Value)||City.Value=="所有地区") ? "" : string.Format((string.IsNullOrEmpty(strWhere.ToString()) ? "" : " AND ") + " T_City='{0}' ", City.Value.Trim()));
            return strWhere.ToString();
        }
        /// <summary>
        /// 绑定用户信息
        /// </summary>
        private void LoadService()
        {
            //取得权限信息
            string PowerMes = Common.EncryptString.DeMyEnncrypt(Common.GetMes.GetCookies("Power_Mes"), 6);
            if (PowerMes.IndexOf("1_3") < 0)
            {
                ListItem LTUser = new ListItem(Common.EncryptString.DeMyEnncrypt(Common.GetMes.GetCookies("User_Name"), 6), Common.GetMes.GetSession("User_ID"));
                DDlService.Items.Clear();
                DDlService.Items.Add(LTUser);
            }
            else
            {
                DDlService.DataSource = BLL.ServiceUser.GetAllServiceUserInfo("");
                DDlService.DataTextField = "T_ServiceName";
                DDlService.DataValueField = "T_ServiceID";
                DDlService.DataBind();
            }
        }

        protected void BtnAllDel_Click(object sender, EventArgs e)
        {
            //判断用户是否选中了一个或多个客户信息
            if (!string.IsNullOrEmpty(HSelectID.Value.Trim()))
            {   //调用DelUserIDByID将信息删除
                if (BLL.ServiceUser.DelUserIDByID(HSelectID.Value.Trim()))
                {  //提示删除成功并重定向页面
                    Common.MessageBox.Show(Page, 
                        "指定信息成功删除..", 
                        "UserService.aspx", "suess");
                }
                else
                {  //提示删除失败
                    Common.MessageBox.Show(Page, 
                        "指定信息删除失败，请重试或联系技术人员..", 
                        "error");
                }
            }
        }

        /// <summary>
        /// 获取更新的语句
        /// </summary>
        private string GetUpdateMes()
        {  //定义保存更新语句的StringBuilder对象
            StringBuilder strUpdate = new StringBuilder();
            //添加更新字段
            strUpdate.Append((DDlService.SelectedValue == "-1") ? "" : 
                string.Format((!string.IsNullOrEmpty(strUpdate.ToString()) ? "," : " SET ") + 
                " T_NowService='{0}',T_UserState=1 ", DDlService.SelectedItem.Text.Trim()));
            return strUpdate.ToString(); //返回更新字符串
        }
        protected void BtnUpdate_Click(object sender, EventArgs e)
        {   //判断隐藏字段中是否包含了选中的客户Id
            if (!string.IsNullOrEmpty(HSelectID.Value.Trim()))
            {
                if (BLL.ServiceUser.UpdateUserIDByID
                    (HSelectID.Value.Trim(),GetUpdateMes()))
                {
                    //如是跟新归属 则给工作记录表里写入数据
                    if (DDlService.SelectedIndex>0)
                    {   //在跟踪日志表里面写入跟踪消息
                        BLL.ServiceUser.InsertWorkLog
                            (HSelectID.Value.Trim(), DDlService.SelectedValue);
                    }
                    LoadData();   //重新加载数据
                    //提示更新成功
                    Common.MessageBox.Show
                        (Page, "指定信息成功更新..", "suess");
                }
                else
                {
                    Common.MessageBox.Show
                        (Page, "指定信息删除失败，请重试或联系技术人员..", "error");
                }
            }
        }
    }
}
